package top.moonkit.rs_pro.patterns.CheckCode;

import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.moonkit.rs_pro.exception.BusinessException;
import top.moonkit.rs_pro.properties.EMailProperty;
import top.moonkit.rs_pro.properties.SmsProperty;
import top.moonkit.rs_pro.utils.Mu;

import java.security.GeneralSecurityException;

/**
 * @author moon
 * @since
 */
@Service
public class EMailCheckCodeProcessor extends CheckCodeService{

    @Autowired
    private EMailProperty eMailProperty;

    @Override
    public boolean isMe(String account) {
        if (account.contains("@")){
            ACCOUNT_CONTENT.set(account);
            return true;
        }
        return false;
    }

    @Override
    void send(String code) {

            // 使用hutool发送邮件
            MailAccount account = new MailAccount();
            account.setHost(eMailProperty.getHost());
            account.setPort(eMailProperty.getPort());
            account.setAuth(true);
            account.setFrom(eMailProperty.getUsername());
            account.setUser(eMailProperty.getUsername());
            account.setPass(eMailProperty.getPassword());

//            MailSSLSocketFactory sf = new MailSSLSocketFactory();
//            sf.setTrustAllHosts(true);
//            account.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
        String toEmail = ACCOUNT_CONTENT.get();
        MailUtil.send(
                    account,
                toEmail,
                    Mu.m("app.name")+Mu.m("check_code"),
                    Mu.m("your")+" "+Mu.m("check_code")+" "+ Mu.m("is")+"：<h3>"+code+"</h3>",
                    true);



    }
}
